package com.hyf.educms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyf.commonutils.fruit;
import com.hyf.educms.entity.CrmBanner;
import com.hyf.educms.service.CrmBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器  管理员使用的接口
 * 后台 管理接口
 * </p>
 *
 * @author atguigu
 * @since 2021-06-28
 */
@Api(description="轮播图管理员")
@RestController
@RequestMapping("/educms/banneradmin")
//@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;


    // 分页查询 banner
    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("pageBanner/{current}/{limit}")
    public fruit pageBanner(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "当前页码", required = true)
            @PathVariable long limit){

        //分页
        IPage<CrmBanner> pageBanner=new Page<>(current, limit);
    
  
         //调用方法
        bannerService.page(pageBanner,null);
        long total=pageBanner.getTotal();
        System.out.println("页数："+total);

        // 数据list集合
       List<CrmBanner> records= pageBanner.getRecords();
        
        
        return fruit.ok().data("total",total).data("items",records);
    }
    
    
 
    
    
    
    
// 根据id查询 Banner
    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public fruit getBannerId(@PathVariable String id){

        CrmBanner crmBanner=bannerService.getById(id);

        return fruit.ok().data("item",crmBanner);
    }



// 添加
    @ApiOperation(value = "新增Banner")
    @PostMapping("addBanner")
    public fruit addBanner(@RequestBody CrmBanner crmBanner){
        // 调用添加的方法
        bannerService.save(crmBanner);

        return fruit.ok();
    }

    // 修改 Banner
    @ApiOperation(value = "修改Banner")
    @PostMapping("updateBanner")
    public fruit updateBanner(@RequestBody CrmBanner banner){

        bannerService.updateById(banner);

        return fruit.ok();
    }


// 删除
@ApiOperation(value = "删除Banner")
@DeleteMapping("remove/{id}")
    public fruit deleteBanner(@PathVariable String id) {

        bannerService.removeById(id);

        return fruit.ok();

}




    // 课件中的方法
/*    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("{page}/{limit}")
    public fruit index(
@ApiParam(name = "page", value = "当前页码", required = true)
        @PathVariable Long page,

@ApiParam(name = "limit", value = "每页记录数", required = true)

        @PathVariable Long limit) {

        Page<CrmBanner> pageParam = new Page<>(page, limit);
        bannerService.pageBanner(pageParam,null);
        return fruit.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }*/


  /*  @ApiOperation(value = "新增Banner")
    @PostMapping("save")
    public fruit save(@RequestBody CrmBanner banner) {

        bannerService.saveBanner(banner);

        return fruit.ok();

    }


    @ApiOperation(value = "修改Banner")

    @PutMapping("update")

    public fruit updateById(@RequestBody CrmBanner banner) {

        bannerService.updateBannerById(banner);

        return fruit.ok();

    }


    @ApiOperation(value = "删除Banner")

    @DeleteMapping("remove/{id}")

    public fruit remove(@PathVariable String id) {

        bannerService.removeBannerById(id);

        return fruit.ok();

    }

      // 根据id 查询
    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public fruit get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return fruit.ok().data("item", banner);
    }

    */

}

